


from generate_benchmark import loadBenchmark
from engine import Engine
from quantum_scheduler import all_cases, filterCases
from config import *

class QuantumEngine(Engine):

    def step(self):
        print('in step:', self.now_time)

        self.checkNetwork()

        required_assigned_tasks = self.checkExecutingTasks()


        queue_sizes = [self.node2queue[node].qsize() for node in range(self.node_number)]
        data_sizes = [ 
            required_assigned_tasks[node].data_size
            if node in required_assigned_tasks
            else 0
            for node in range(self.node_number)
        ]
        
        # [  for from_node, task  in .items()]
        cases = filterCases(lambda case: queue_sizes == case.queue_sizes and data_sizes == case.data_sizes)

        print(len(cases))
        cases = filterCases(lambda case: all([ 
            case.chosed_nodes[node_number] != 0 if node in required_assigned_tasks else case.chosed_nodes[node_number] == 0
            for node in range(self.node_number)
        ]), cases)

        print(len(cases))


        required_assigned_tasks = self.checkExecutingTasks()

        for from_node, task  in required_assigned_tasks.items():
            assign_node =  self.assignNode(task, from_node)
            print(task, 'is called by', task, 'in', from_node, 'assigned to', assign_node)
            self.sendTask(task, from_node, assign_node)

        self.now_time += 1
        return



if __name__ == "__main__":
    engine = QuantumEngine(node_number)
    engine.init('bechmark100_5.txt')

    while not engine.isFinish():
        engine.step()

    engine.summaryResult()


    